package com.dms.modules.user.util;

import com.dms.modules.user.entity.UserBrowseHistory;
import com.dms.modules.product.entity.ProductFavorite;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户数据转换工具类
 */
public class UserDataConverter {
    
    /**
     * 将Map转换为UserBrowseHistory对象
     */
    public static UserBrowseHistory toBrowseHistory(Map<String, Object> map) {
        if (map == null) {
            return null;
        }
        UserBrowseHistory history = new UserBrowseHistory();
        history.setId((Long) map.get("id"));
        history.setUserId((Long) map.get("userId"));
        history.setProductId((Long) map.get("productId"));
        history.setCategoryId((Long) map.get("categoryId"));
        history.setViewCount((Integer) map.get("viewCount"));
        history.setStayDuration((Integer) map.get("stayDuration"));
        history.setLastViewTime((LocalDateTime) map.get("lastViewTime"));
        history.setCreateTime((LocalDateTime) map.get("createTime"));
        history.setUpdateTime((LocalDateTime) map.get("updateTime"));
        history.setDeleted((Integer) map.get("deleted"));
        return history;
    }
    
    /**
     * 将Map列表转换为UserBrowseHistory列表
     */
    public static List<UserBrowseHistory> toBrowseHistoryList(List<Map<String, Object>> maps) {
        if (maps == null) {
            return null;
        }
        return maps.stream()
                .map(UserDataConverter::toBrowseHistory)
                .collect(Collectors.toList());
    }
    
    /**
     * 将Map转换为ProductFavorite对象
     */
    public static ProductFavorite toFavorite(Map<String, Object> map) {
        if (map == null) {
            return null;
        }
        ProductFavorite favorite = new ProductFavorite();
        favorite.setId((Long) map.get("id"));
        favorite.setUserId((Long) map.get("userId"));
        favorite.setProductId((Long) map.get("productId"));
        favorite.setCreateTime((LocalDateTime) map.get("createTime"));
        return favorite;
    }
    
    /**
     * 将Map列表转换为ProductFavorite列表
     */
    public static List<ProductFavorite> toFavoriteList(List<Map<String, Object>> maps) {
        if (maps == null) {
            return null;
        }
        return maps.stream()
                .map(UserDataConverter::toFavorite)
                .collect(Collectors.toList());
    }
} 